package com.padakeji.android.pworks.content;

import android.graphics.Bitmap;
import android.os.Environment;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by atanl on 2015/11/21.
 */
public class FileUtils {

    public static boolean isSdCardMounted() {
        return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
    }

    public static boolean saveBitmapToTempFile(File file, Bitmap bitmap){
        BufferedOutputStream bos= null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    public static File saveBitmapToTempFile(Bitmap bitmap){
        File file = newTempImageFile();
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static File newTempImageFile() {
        File image = null;
        String imageFileName = "JPEG_" + UUID.randomUUID().toString();
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        if (!storageDir.exists()) {
            if (!storageDir.mkdir()) {
                return image;
            }
        }
        try {
            image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );
        } catch (IOException e) {
            e.printStackTrace();
        }

        return image;
    }

    public static boolean deleteRecursively(String path) throws FileNotFoundException {
        File file = new File(path);
        if (!file.exists()) {
            throw new FileNotFoundException("File in the path :" + path + " not exists.");
        }
        if (!file.isDirectory()) {
            return file.delete();
        }
        File[] children = file.listFiles();
        if (children != null && children.length != 0) {
            for (int i = 0; i < children.length; ++i) {
                if (!deleteRecursively(children[i].getPath())) {
                    return false;
                }
            }
        }
        return file.delete();
    }

}
